<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 6/20/24
 * Time: 2:11 PM
 */

namespace App\Http\Controllers\Admin;

use App\Services\UploadService;
use Illuminate\Http\Request;
use App\Http\Controllers\ResourceController;

class UploadController extends ResourceController
{

    /**
     * 上传文件
     *
     * @param Request $request
     * @return mixed
     */
    public function upload(Request $request)
    {
        if (!$file = $request->file('file', '')) {

            return $this->responseWithError('请选择要上传的文件');
        }
        $result = UploadService::upload($file);
        if ($result['err'] ?? '' !== 0) {

            return $this->responseWithError($result['msg'] ?? '上传失败');
        }

        return $this->responseWithSuccess(['file_path' => $result['filepath'] ?? '']);
    }
}
